# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql
pymysql.install_as_MySQLdb()

class DoubanmvPipeline(object):
    def __init__(self):
        # 连接MySQL数据库
        self.connect = pymysql.connect(host='localhost',
                                       user='root', password='Lj970914',
                                       db='douban', port=3306)  # db要和建的数据库名称对应上
        self.cursor = self.connect.cursor()

    def process_item(self, item, spider):
        self.cursor.execute('delete from scrapy')
        # for name in item['names']:
        #     for star in item['stars']:
        sql = 'insert into scrapy(name,star)VALUES(%s, %s)'%(item['names'],item['stars'])
        self.cursor.execute(sql)
            #     continue
            # continue

        self.connect.commit()
        return item

    def close_spider(self, spider):
        self.cursor.close()
        self.connect.close()
